const ACCESS_TOKEN = 'access_token';
const TOKEN_EXPIRE_TIME = 'token_expire_time';
const ACCESS_REFRESH_TOKEN = 'refresh_token';
const SSO_TYPE = 'sso_type';

/**
 * 计算token过期时间时间戳
 * @param {*} seconds
 * @returns
 */
function setExpireTime(seconds) {
  const currentTime = new Date();
  const milliseconds = seconds * 1000; // 将秒数转换为毫秒
  const newTime = new Date(currentTime.getTime() + milliseconds);
  return newTime.getTime();
}
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function getParameterFromURL(parameterName) {
  const regex = new RegExp(`[?&]${parameterName}(=([^&#]*)|&|#|$)`);
  const results = regex.exec(window.location.href);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
/**
 * 设置登录token信息
 * @param {*} result
 */
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function storeData(result, Cookies) {
  // 记单点登录类型
  Cookies.set(`${window.$config.cookies.namespace}${ACCESS_TOKEN}`, result.token);
  Cookies.set(`${window.$config.cookies.namespace}${ACCESS_REFRESH_TOKEN}`, result.refreshToken);
  const timestamp = setExpireTime(result.expireTime);
  Cookies.set(`${window.$config.cookies.namespace}${TOKEN_EXPIRE_TIME}`, timestamp);
}
/**
 * 清除token并设置单点类型
 * @param {*} SSOType
 * @param {*} Cookies
 */
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function setSSOType(SSOType, Cookies) {
  // 清除token
  Cookies.remove(`${window.$config.cookies.namespace}${ACCESS_TOKEN}`);
  Cookies.remove(`${window.$config.cookies.namespace}${ACCESS_REFRESH_TOKEN}`);
  // 记单点登录类型
  localStorage.setItem(SSO_TYPE, SSOType);
}
